package com.app.twth.ui.b2bpick;


import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.app.twth.BR;
import com.app.twth.R;
import com.app.twth.app.AppRouter;
import com.app.twth.app.Constant;
import com.app.twth.base.BaseActivity;
import com.app.twth.databinding.ActivityB2bPickBinding;
import com.app.twth.databinding.DialogB2bpickDetailBinding;
import com.app.twth.net.OkHttpUtils;
import com.app.twth.ui.b2bpick.category.B2BPickCategoryActivity;
import com.app.twth.utils.DividerItemDecoration;
import com.app.twth.utils.EditTextUtil;
import com.app.twth.utils.MyDialogUtil;
import com.app.twth.utils.PreferencesUtil;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

/**
 * @describe :
 */
public class B2BPickActivity extends BaseActivity<ActivityB2bPickBinding, B2BPickViewModel> implements OnRefreshListener {
    private String TAG = getClass().getSimpleName();
    private B2BPickAdapter pickAapter;

    @Override
    protected void initListener() {
        viewModel.pickItemLiveData.observe(this, data -> {
            showTallyDetailDialog(data);
        });
        viewModel.pickLiveData.observe(this, data -> {
            Bundle bundle = new Bundle();
            bundle.putSerializable("B2BPick", data);
            AppRouter.showActivityForResult(mContext, B2BPickCategoryActivity.class, bundle, Constant.ForResult2);
        });
        viewModel.pickListLiveData.observe(this, data -> {
            //关闭下拉
            binding.refreshLayout.finishRefresh();
            pickAapter.setData(data, 0);
        });

        viewModel.errorResultCode.observe(this, code -> {

            //关闭下拉
            binding.refreshLayout.finishRefresh();
            showHint((String) code);
        });
        viewModel.isLoading.observe(this, b -> {
            if (b.isShow()) {
                showProgressDialog(b.getMsg());
            } else {
                dismissProgressDialog();
            }
        });
    }

    protected String setTitle() {
        return "Pick(Scan Job)";
    }

    @Override
    protected int initViewModelId() {
        return BR.viewModel;
    }

    @Override
    protected void initData() {
        setLayoutBottomContentGone();
        pickAapter = new B2BPickAdapter(this, new ArrayList<>(), position -> {
            viewModel.b2bPickCheckPickNo(viewModel.pickListLiveData.getValue().get(position));
        }, position -> {
            viewModel.b2bPickItems(viewModel.pickListLiveData.getValue().get(position));
        });
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        binding.recyclerView.setLayoutManager(layoutManager);
        //添加装饰类
        binding.recyclerView.addItemDecoration(new DividerItemDecoration(mContext, DividerItemDecoration.VERTICAL_LIST));
        binding.recyclerView.setAdapter(pickAapter);

        binding.refreshLayout.setOnRefreshListener(this);
        binding.refreshLayout.autoRefresh();//触发自动刷新
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
    }

    public void onViewClicked(View view) {
        switch (view.getId()) {
        }
    }

    @Override
    public void getScanninCode() {
        super.getScanninCode();
    }

    private AlertDialog dialogTallyDetail;

    private void showTallyDetailDialog(B2BPick data) {
        dialogTallyDetail = MyDialogUtil.getDialog(mContext, initTallyDetailDialogView(data), Gravity.CENTER);
        dialogTallyDetail.setCanceledOnTouchOutside(true);
        dialogTallyDetail.show();
    }

    private View initTallyDetailDialogView(B2BPick b2BPick) {
        List<B2BPick.Details> data = b2BPick.getList();
        DialogB2bpickDetailBinding dialogBinding = DialogB2bpickDetailBinding.inflate(getLayoutInflater());
        dialogBinding.tvTitle.setText("" + b2BPick.getPickNo());
        dialogBinding.etPassword.setVisibility(View.VISIBLE);
        dialogBinding.tvPasswordHint.setVisibility(View.VISIBLE);
        B2BPickDetailsAdapter tallyDetailAdapter = new B2BPickDetailsAdapter(this, data);

        LinearLayoutManager layoutManager1 = new LinearLayoutManager(this);
        layoutManager1.setOrientation(LinearLayoutManager.VERTICAL);
        dialogBinding.recyclerviewDetail.setLayoutManager(layoutManager1);
        dialogBinding.recyclerviewDetail.setAdapter(tallyDetailAdapter);

        dialogBinding.tvCancel.findViewById(R.id.tv_cancel).setOnClickListener(v -> {
            if (dialogTallyDetail != null) {
                dialogTallyDetail.dismiss();
            }
        });
        dialogBinding.tvConfirm.findViewById(R.id.tv_confirm).setOnClickListener(v -> {
            String password = dialogBinding.etPassword.getText().toString();
            if (TextUtils.isEmpty(password)) {
                dialogBinding.tvPasswordHint.setText("enter password");
                return;
            }
            String password_default = PreferencesUtil.getString(this, Constant.submit_password);
            if (TextUtils.isEmpty(password_default)) {
                dialogBinding.tvPasswordHint.setText("No permission");
                OkHttpUtils.getInstance(mContext).getPdaPassword();
                return;
            }
            if (!password_default.equals(password)) {
                dialogBinding.tvPasswordHint.setText("wrong password");
                OkHttpUtils.getInstance(mContext).getPdaPassword();
                return;
            }
            EditTextUtil.setKeyboardState(mContext, EditTextUtil.HIDE_KEYBOARD, dialogBinding.etPassword);

            if (dialogTallyDetail != null) {
                dialogTallyDetail.dismiss();
            }
            viewModel.b2bPickCompleted(b2BPick);
        });
        return dialogBinding.getRoot();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        bottomLayoutBinding.tvHint.setText("");
        viewModel.b2bPickPendingJobs();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == Constant.ForResult2 && resultCode == RESULT_OK) {
            binding.refreshLayout.autoRefresh();//触发自动刷新
        }
    }

}
